package com.lagou.job.config;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 应用上下文支撑类 ：提供获得Bean的快捷方法
 * @author yzh
 */
@Component
public class ApplicationContextHolder implements ApplicationContextAware {

    private static ApplicationContext ac;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ac = applicationContext;
    }

    /**
     * 获得Bean容器
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return ac;
    }

    /**
     * 根据类字节码获得Bean
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return ac.getBean(clazz);
    }

    /**
     * 根据Bean名称获得Bean
     * @param name
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name) {
        return (T)ac.getBean(name);
    }
}
